package com.michael.simulation.spring.client;

import com.michael.simulation.spring.beans.BeanDefinition;
import com.michael.simulation.spring.beans.DefaultBeanFactory;
import com.michael.simulation.spring.beans.GenericBeanDefinition;
import com.michael.simulation.spring.client.service.UserService;

/**
 * 类功能描述:
 * <pre>
 *   测试
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/3 17:39
 */
public class Client {


    static DefaultBeanFactory df = new DefaultBeanFactory();

    public static void main(String[] args) throws Exception {

        testRegister();
    }

    public static void testRegister() throws Exception {

        //创建bean定义
        GenericBeanDefinition bd = new GenericBeanDefinition();

        //设置bean的类名
        bd.setBeanClass(UserService.class);
        //设置是否单例
        bd.setScope(BeanDefinition.SCOPE_SINGLETION);
        //bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);

        bd.setInitMethodName("init");
        bd.setDestroyMethodName("destroy");

        //把bean定义注册到bean工厂DefaultBeanFactory bf
        df.registerBeanDefinition("userService", bd);

        for (int i = 0; i < 5; i++) {
            UserService userService = (UserService) df.getBean("userService");
            userService.doSomething();
        }

        df.close();
    }
}
